import time

from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from zBase.Base import Base
class Page(Base):
    url='https://re.m.jd.com/page/homelike?ad_od=3&re_dcp=21Sm2D2ZOw&traffic_source=1004&bd_vid=b34156fec1f5ca88&cu=true&utm_source=haosou-search&utm_medium=cpc&utm_campaign=t_262767352_haosousearch&utm_term=72869223856_0_37afd7c0f11c4e7191f3ad7f4d8d2e74'
    #使用XPath定位策略定位首页的商品搜索输入框
    sosuo=(By.XPATH,'//*[@id="jd_search_contain"]/div/div/input')
    #通过CSS选择器定位用户登录按钮
    denglu=(By.CLASS_NAME,'style-red')
    #验证搜索结果页面标题是否包含搜索关键词
    dy1=(By.XPATH,'/html/body/div[4]/div[2]/strong')
    #提取并输出前5个搜索结果的商品名称和价格信息
    sp1=(By.XPATH,'//*[@id="shop_list"]/li[1]')
    sp2=(By.XPATH,'//*[@id="shop_list"]/li[2]')
    sp3 = (By.XPATH, '//*[@id="shop_list"]/li[3]')
    sp4 = (By.XPATH, '//*[@id="shop_list"]/li[4]')
    sp5 = (By.XPATH, '//*[@id="shop_list"]/li[5]')
    #选择按价格排序功能重新排列商品
    px=(By.XPATH,'/html/body/div[6]/div[1]/div[2]/div/div[2]/div/ul/li[1]/a')
    px2=(By.XPATH,'//*[@id="shop_list"]')
    def yx(self):
        self.get_url(self.url)
        time.sleep(5)
        self.send_keys(self.sosuo,'智能手机')
        time.sleep(3)
        self.send_keys(self.sosuo,Keys.ENTER)
        time.sleep(5)
        self.qh()
        a=self.text(self.dy1)
        assert '智能手机' in a
        time.sleep(5)
        #对搜索结果列表区域进行截屏保存
        self.jt()
        time.sleep(3)
        print(self.text(self.sp1))
        time.sleep(2)
        print(self.text(self.sp2))
        time.sleep(2)
        print(self.text(self.sp3))
        time.sleep(2)
        print(self.text(self.sp4))
        time.sleep(2)
        print(self.text(self.sp5))
        time.sleep(2)
        self.click(self.px)
        time.sleep(2)
        c=self.text(self.px2)
        with open('min.log','w+',encoding='utf8')as f:
            f.write(c)
            f.close()
        time.sleep(2)
